package com.youth.vod.service.impl;

import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.youth.commonutils.R;
import com.youth.servicebase.exceptionhandler.GlobalException;
import com.youth.vod.service.VodService;
import com.youth.vod.utils.AliyunVodSDKUtils;
import com.youth.vod.utils.ConstantPropertiesUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

@Service
@Slf4j
public class VodServiceImpl implements VodService {


    /*上传小节视频*/
    @Override
    public String uploadVideo(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            /*获取视频标题*/
            String title = originalFilename.substring(0, originalFilename.lastIndexOf("."));
            /*封装请求信息并发送*/
            UploadStreamRequest request = new UploadStreamRequest(
                    ConstantPropertiesUtil.ACCESS_KEY_ID,
                    ConstantPropertiesUtil.ACCESS_KEY_SECRET,
                    title, originalFilename, inputStream);
            /*手动设置地域，否则不成功*/
            request.setApiRegionId("cn-shenzhen");
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            /*如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。*/
            /*其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因*/
            String videoId = response.getVideoId();
            if (!response.isSuccess()) {
                String errorMessage = "阿里云上传错误：" + "code：" + response.getCode() + ", message：" + response.getMessage();
                log.warn(errorMessage);
                /*id为空,抛出自定义异常*/
                if (StringUtils.isEmpty(videoId)) {
                    throw new GlobalException(20001, errorMessage);
                }
            }
            return videoId;
        } catch (IOException e) {
            throw new GlobalException(20001, "小节视频上传失败");
        }

    }

    /*根据视频ID删除阿里云上的视频*/
    @Override
    public R removeVideo(String videoSourceId) {
        try {
            /*使用工具类获得请求客户端*/
            DefaultAcsClient client = AliyunVodSDKUtils.initVodClient(ConstantPropertiesUtil.ACCESS_KEY_ID, ConstantPropertiesUtil.ACCESS_KEY_SECRET);
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(videoSourceId);
            /*封装请求并响应*/
            DeleteVideoResponse response = client.getAcsResponse(request);
            log.info("\"RequestId = \" + response.getRequestId() + \"\\n\"");
        } catch (ClientException e) {
            throw new GlobalException(20001, "视频删除失败");
        }
        return R.ok();
    }
}